<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        //利用对象封装自己的数学对象  里面有PI 最大值 最小值
        var myMath = {
            PI: 3.141592653,
            max: function () {
                var max = arguments[0];  //arguements可以接受任意多个值
                for (var i = 1; i < arguments.length; i++) {
                    if (arguments[i] > max) {
                        max = arguments[i];
                    }
                }
                return max;
            },
            min: function () {
                var min = arguments[0];
                for (var i = 1; i < arguments.length; i++) {
                    if (arguments[i] < min) {
                        min = arguments[i];
                    }
                }
                return min;
            }
        }
        console.log(myMath.PI);
        console.log(myMath.max(2, 7, 4, 9));
        console.log(myMath.min(2, 7, 4, 9));

        //math的其他用法
        //绝对值方法
        console.log(Math.abs(1));  //1
        console.log(Math.abs(-1));   //1
        console.log(Math.abs('-1'));//1 隐式转换，会把字符串-1转换为数字型
        console.log(Math.abs('pink'));  //NaN

        //三个取整方法
        //Math.floor()  向下取整 往最小了取值
        console.log(Math.floor(1.1));  //1   floor地板
        console.log(Math.floor(1.9));  //1
        // Math.ceil()  向上取整  往最大了取值
        console.log(Math.ceil(1.1));  //2    ceil天花板
        console.log(Math.ceil(1.9));  //2
        //Math.round()  四舍五入
        console.log(Math.round(1.1));  //1
        console.log(Math.round(1.5));  //2
        console.log(Math.round(1.9));  //2
        console.log(Math.round(-1.1));  //-1
        console.log(Math.round(-1.5));  //-1  因为-1比-2大

        //random随机数方法
        console.log(Math.random());  //不跟参数,返回一个0<=x<1的随机小数[0,1)
        function getRandom(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        }
        console.log(getRandom(1, 10));
        //例子随机点名
        var arr = ['张三', '王五', '黎明', '刘华', '李淼', '沈梦'];
        console.log(arr[getRandom(0, arr.length - 1)]);


        //猜数字游戏
        function getRandom(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        }
        var random = getRandom(1, 50);
        while (i = 1, i <= 10, i++) {
            var num = prompt('请猜一个1~50之间的一个数字');
            if (num > random) {
                alert('大了');
            } else if (num < random) {
                alert('小了');
            } else {
                alert('对了');
                break;
            }
        }
    </script>
</head>

<body>

</body>

</html>